<?php 
// this file is to add items to inventory
// connect to mysql database
require("../storescripts/config.php");
//start session
session_start();

if (!isset($_SESSION["manager"])) {
    header("location: admin_login.php"); 
    exit();
}
?>
<?php 
// parse the data and add to inventory
if (isset($_POST['product_name'])) {
	
	//sanitize variables
    $product_name = mysql_real_escape_string($_POST['product_name']);
	$price = mysql_real_escape_string($_POST['price']);
	$category = mysql_real_escape_string($_POST['category']);
	$subcategory = mysql_real_escape_string($_POST['subcategory']);
	$subcategory1 = mysql_real_escape_string($_POST['subcategory1']);
	$details = mysql_real_escape_string($_POST['details']);
	$is_featured = mysql_real_escape_string($_POST['isFeatured']);
	$quantity = mysql_real_escape_string($_POST['quantity']);
	
	
	// check if the product has a match on database
	$sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
	
	// add this product into the database 
	$sql = mysql_query("INSERT INTO products (product_name, price, details, category, subcategory, date_added,  subcategory1, is_featured, original_quantity, quantity) 
        VALUES('$product_name','$price','$details','$category','$subcategory', now(), '$subcategory1', '$is_featured', '$quantity', '$quantity')") or die (mysql_error());
     $pid = mysql_insert_id();
	// place uploaded image in the folder 
	$newname = "$pid.jpg";
	move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
	header("location: inventory_list.php"); 
    exit();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inventory List</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen" />

<!-- start of javascript -->
<script type="text/javascript" language="javascript"> 
// validating forms before processing to a PHP script
function validateMyForm ( ) { 
    var isValid = true;
    if ( document.myForm.product_name.value == "" ) { 
	    alert ( "Please indicate product name" ); 
	    isValid = false;
    } 	else if ( document.myForm.product_name.value == "<?php echo $product_name; ?>" ) { 
	            alert ( "Product name is duplicated. Please enter another product name." ); 
	            isValid = false;
	} else if ( document.myForm.price.value == "" ) { 
            alert ( "Please indicate product price" ); 
            isValid = false;
    } else if ( document.myForm.quantity.value == "" ) { 
		            alert ( "Please indicate product quantity" ); 
		            isValid = false;
	} else if ( document.myForm.category.value == "" ) { 
		            alert ( "Please indicate subcategory of product" ); 
		            isValid = false;
	} else if ( document.myForm.subcategory.value == "" ) { 
            alert ( "Please indicate subcategory of product" ); 
            isValid = false;
    } else if ( document.myForm.subcategory1.value == "" ) { 
		     alert ( "Please indicate subcategory1 of product" ); 
		     isValid = false;
	}
    return isValid;
}
</script>
<!--end of script -->

</head>

<body>
<!-- Main Shell -->
<div class="shell">
	
  <!-- Header -->
  <div id="header"><h1 id="logo"><a href="index.php">Luvo Fashion</a></h1>
	<div align="right" style="padding-right:5px; padding-top:40px;">
		<a style="color:#ccc;" href="inventory_list.php">Back</a>&nbsp; |
		<a style="color:#ccc;" href="logout.php">Logout</a>
	</div>
  </div>
  <!-- End Header -->

  <!-- Main -->
  <div id="main">
    <div align="center" style="background:#ebebeb; width:800px; margin-left:80px; margin-top:15px; padding-top:5px; padding-bottom:5px;">
    <a name="inventoryForm" id="inventoryForm"></a>
    <h3>&darr; Add New Inventory Item Form &darr;</h3>
    <br />
    <br />
    <form action="inventory_form.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table width="90%" border="0" cellspacing="5" cellpadding="6">
      <tr>
        <td width="20%" align="right">Product Name</td>
        <td width="80%">
		  <label>
          	<input name="product_name" type="text" id="product_name" size="50" />
          </label>
		</td>
      </tr>
      <tr>
        <td align="right">Product Price</td>
        <td>
		  <label>
          	$&nbsp;<input name="price" type="text" id="price" size="12" />
          </label>
		</td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td>
			<label>
          		<select name="category" id="category">
					<option value="">Choose a Category</option>
          			<option value="Men">Men's</option>
          			<option value="Women">Women's</option>
          		</select>
        	</label>
		</td>
      </tr>
      <tr>
        <td align="right">Subcategory</td>
        <td>
			<select name="subcategory" id="subcategory">
        		<option value="">Choose a Subcategory</option>
          		<option value="Clothing">Clothes</option>
          		<option value="Shoes">Shoes</option>
          		<option value="Bags">Bags</option>
				<option value="Accessories">Accessories</option>
          	</select>
		</td>
      </tr>
	  <tr>
        <td align="right">Subcategory 1</td>
        <td>
			<select name="subcategory1" id="subcategory1">
        		<option value="">Choose a Subcategory1</option>
          		<option value="Shirts">Shirts</option>
				<option value="Dress">Dress</option>
          		<option value="Pants">Pants</option>
          		<option value="Shorts">Shorts</option>
          		<option value="Jackets">Jackets</option>
          		<option value="Skirts">Skirts</option>
				<option value="Leather">Leather Shoes</option>
				<option value="Sandals">Sandals</option>
				<option value="Slippers">Slippers</option>
          		<option value="SlingBags">Sling Bags</option>
				<option value="Purses">Purses</option>
          		<option value="Backpacks">Backpacks</option>
				<option value="Watches">Watches</option>
				<option value="Belts">Belts</option>
				<option value="Sunglasses">Sunglasses</option>
          	</select>
		</td>
      </tr>
      <tr>
        <td align="right">Product Details</td>
        <td>
			<label>
          		<textarea name="details" id="details" cols="50" rows="5"></textarea>
        	</label>
		</td>
      </tr>
	<tr>
        <td align="right">Product Quantity</td>
        <td>
			<label>
          		<input name="quantity" type="text" id="quantity" size="5"></textarea>
        	</label>
		</td>
      </tr>
	  <tr>
		<td align="right">Featured Item</td>
		<td><input type="checkbox" name="isFeatured" value="1" /></td>
      </tr> 
	  <tr>
        <td align="right">Product Image</td>
        <td>
			<label>
          		<input type="file" name="fileField" id="fileField" />
        	</label>
		</td>
      </tr>      
      <tr>
        <td>&nbsp;</td>
        <td>
			<label>
          		<input type="submit" name="button" id="button" value="Add This Item Now" onclick="javascript:return validateMyForm();" />
        	</label>
		</td>
      </tr>
    </table>
    </form>
    </div>
  </div>
<!-- End Main -->
  
<!-- Footer -->
   <div style="padding-top:5px;" id="footer">
    <p class="left">Add Inventory</p>
    <p class="right"> &copy; 2010 Luvo Fashion.</p>
    </div>
<!-- End Footer -->

</div>
<!-- End Shell -->

</body>
</html>